﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using QS.Logic.Objects;

namespace DPSF.ParticleSystems
{
	[Serializable]
	class DefaultTexturedQuadParticleSystemTemplate : DefaultTexturedQuadParticleSystem
	{
        private QSpaceShip spaceShip;

		public DefaultTexturedQuadParticleSystemTemplate(Game cGame, QSpaceShip  spaceShip) : base(cGame) 
        {
            this.spaceShip = spaceShip;
        }

		public override void AutoInitialize(GraphicsDevice cGraphicsDevice, ContentManager cContentManager, SpriteBatch cSpriteBatch)
		{
			InitializeTexturedQuadParticleSystem(cGraphicsDevice, cContentManager, 1000, 50000, UpdateVertexProperties, "fire");
			Name = "Default Textured Quad Particle System Template";
            LoadParticleSystem();
		}

        public void LoadParticleSystem()
		{
			ParticleInitializationFunction = InitializeParticleUsingInitialProperties;

			InitialProperties.LifetimeMin = 2.0f;
			InitialProperties.LifetimeMax = 2.0f;
			InitialProperties.PositionMin = Vector3.Zero;
			InitialProperties.PositionMax = Vector3.Zero;
			InitialProperties.VelocityMin = new Vector3(0, 0.1f, 0);
			InitialProperties.VelocityMax = new Vector3(0, 0.4f, 0);
			InitialProperties.RotationMin = new Vector3(0, 0, 0);
			InitialProperties.RotationMax = new Vector3(0, 0, MathHelper.Pi);
			InitialProperties.RotationalVelocityMin = new Vector3(0, 0, -MathHelper.Pi);
			InitialProperties.RotationalVelocityMax = new Vector3(0, 0, MathHelper.Pi);
			InitialProperties.StartWidthMin = 0.1f;
			InitialProperties.StartWidthMax = 0.40f;
			InitialProperties.StartHeightMin = 0.10f;
			InitialProperties.StartHeightMax = 0.40f;
			InitialProperties.EndWidthMin = 0.10f;
			InitialProperties.EndWidthMax = 0.10f;
			InitialProperties.EndHeightMin = 0.10f;
			InitialProperties.EndHeightMax = 0.10f;
			InitialProperties.StartColorMin = Color.Black;
			InitialProperties.StartColorMax = Color.White;
			InitialProperties.EndColorMin = Color.Black;
			InitialProperties.EndColorMax = Color.White;

			ParticleEvents.RemoveAllEvents();
			ParticleSystemEvents.RemoveAllEvents();

			ParticleEvents.AddEveryTimeEvent(UpdateParticlePositionUsingVelocity);
			ParticleEvents.AddEveryTimeEvent(UpdateParticleRotationUsingRotationalVelocity);
			ParticleEvents.AddEveryTimeEvent(UpdateParticleWidthAndHeightUsingLerp);
			ParticleEvents.AddEveryTimeEvent(UpdateParticleColorUsingLerp);

			ParticleEvents.AddEveryTimeEvent(UpdateParticleTransparencyToFadeOutUsingLerp, 100);
			ParticleEvents.AddEveryTimeEvent(UpdateParticleToFaceTheCamera, 200);
            ParticleSystemEvents.AddEveryTimeEvent(UpdateParticleSystemFunctionExample, 300);

			ParticleSystemEvents.LifetimeData.EndOfLifeOption = CParticleSystemEvents.EParticleSystemEndOfLifeOptions.Repeat;
			ParticleSystemEvents.LifetimeData.Lifetime = 1.0f;
			ParticleSystemEvents.AddTimedEvent(0.0f, UpdateParticleSystemEmitParticlesAutomaticallyOn);
            

			Emitter.ParticlesPerSecond = 50;
			Emitter.PositionData.Position = new Vector3(0, 0, 0);
		}

        protected void UpdateParticleSystemFunctionExample(float fElapsedTimeInSeconds)
        {
            Emitter.PositionData.Position = spaceShip.Position + Vector3.Transform(Vector3.Backward * 0.9f + Vector3.Up * 0.1f, this.spaceShip.Orientation);
            Emitter.OrientationData.Up = spaceShip.Orientation.Up;
            Emitter.OrientationData.Right = spaceShip.Orientation.Right;
            Emitter.OrientationData.Normal = spaceShip.Orientation.Up;
        }
	}
}
